package com.lening.controller;

import com.lening.pojo.TbSpecification;
import com.lening.pojogroup.Specification;
import com.lening.sellergoods.service.SpecificationService;
import entity.PageResult;
import entity.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("specification")
public class SpecificationController {

    @Resource
    private SpecificationService specificationService;

    @RequestMapping("getSpecList")
    public List<Map> getSpecList(){
        List<Map> maps = specificationService.selectOptionList();
        return maps;
    }

    @RequestMapping("search")
    public PageResult search(Integer pageNum, Integer pageSize, @RequestBody TbSpecification searchEntity){
        PageResult page = specificationService.search(pageNum, pageSize,searchEntity);
        return page;
    }

    @RequestMapping("findOne")
    public Specification findOne(Long id){
        Specification one = specificationService.findOne(id);
        return one;
    }

    @RequestMapping("save")
    public Result saveBrand(@RequestBody Specification specification){
        int res = 0;
        try {
           if(specification!=null){
               if(specification.getSpecification().getId()!=null){
                   //修改
                   specificationService.update(specification);
               }else{
                   //添加
                   specificationService.add(specification);
               }

           }
            res = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res>0?new Result(true,"保存成功"):new Result(false,"保存失败");
    }

    @RequestMapping("deleteSpecification")
    public Result deleteSpecification(@RequestBody Long[] ids){
       int res = 0;
        try {
            specificationService.delete(ids);
            res = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res>0?new Result(true,"删除成功"):new Result(false,"删除失败");
    }

}
